//
//  Collection_TableViewCell.swift
//  zhende
//
//  Created by 陆文宇 on 2017/5/5.
//  Copyright © 2017年 chenliang. All rights reserved.
//

import UIKit

class Collection_TableViewCell: UITableViewCell, UICollectionViewDelegate, UICollectionViewDataSource {
    
    deinit {
        yy_Log("\(self) deinit")
    }
    
    weak var viewController: UIViewController!
    
    @IBOutlet weak var cityCollectionView: UICollectionView!
    var cities = [Any]() {
        didSet {
            cityCollectionView.reloadData()
        }
    }

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
        
        let layout = cityCollectionView.collectionViewLayout as! UICollectionViewFlowLayout
        layout.itemSize = CGSize(width: (screenWidth - 60) / 3, height: 40)
        cityCollectionView.collectionViewLayout = layout
        
        cityCollectionView.register(UINib(nibName: "SubCityCell", bundle: nil), forCellWithReuseIdentifier: "cityCell")
    }
    
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return cities.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cityCell", for: indexPath) as! SubCityCell
        let city = cities[indexPath.item]
        cell.cityName.text = city is String ? city as! String : (city as! YYCity).name
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let vc = viewController as! ChooseCityViewController
        let city = cities[indexPath.item]
        if city is String {
            return
        }
        vc.chooseCityClosure(city as! YYCity)
        vc.backAction()
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
}
